﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using Beyond;
using Beyond.Ant;
using WangSir;
using System;
using UnityEngine.EventSystems;
using ZJ_WangSir;

public class NewInPromotions : MonoBehaviour
{
    public static NewInPromotions Instance;

    public Image mInpromotions_Icon;
    public Image mInpromotions_Picture;
    public Image cancelBtn;
    public Image Cancel_GD;
    public Image blackBack;
    public GameObject mInpromotions_TC;
    public GameObject Judge;
    public GameObject content;
    public List<InPromotion> InPromotionList = new List<InPromotion>();
    public List<GameObject> indicators = new List<GameObject>();
    public float pictureRate;
    public Texture2D unactive2D;
    public Texture2D active2D;
    public float pictureInterval;

    int Icon_Value;
    int Picture_Value;
    bool getImage;
    bool pointBool;
    DateTime dt;
    DateTime waitdt;
    bool load;
    bool getImageKP;
    bool bannerBool;
    // Use this for initialization

    void Awake()
    {
        Instance = this;
    }
    void Start()
    {
        ZJ_WangSir.CUtils.CloseBannerAd();
        if (GameState.InpStyle == InPromotion.Style.FullPortrait)
        {
            pictureInterval = 640;
        }
        else if (GameState.InpStyle == InPromotion.Style.FullLandscape)
        {
            pictureInterval = 1280;
        }
        InPromotionList = InPromotion.getInPromotions();
        print(InPromotionList.Count + "*-*-*-*-*-*");
        mInpromotions_Icon.gameObject.SetActive(false);
        mInpromotions_Picture.gameObject.SetActive(false);
        load = false;
        waitdt = DateTime.Now;
        if (!GameState.isStartJoin)
        {
            GetInPromotions_KP();
            GameState.isStartJoin = true;
        }
        GetInPromotions_GD();
        //MyDebug.Add("InPromotionList", InPromotionList.Count);
    }

    // Update is called once per frame
    void Update()
    {
        if (!EventSystem.current.IsPointerOverGameObject() && ((DateTime.Now - waitdt).TotalSeconds) >= 5f && !pointBool)
        {
            mInpromotions_Picture.gameObject.SetActive(false);
            pointBool = true;
        }

        UpdateIndicators();

        if ((DateTime.Now - waitdt).TotalSeconds >= 0.5f && !load)
        {
            CreatPicture();
            load = true;
        }

        if (!bannerBool && !mInpromotions_TC.activeInHierarchy && !mInpromotions_Picture.gameObject.activeInHierarchy)
        {
            //ZJ_WangSir.CUtils.ShowBannerAd();
            bannerBool = true;
        }

        if (Input.GetKeyDown(KeyCode.Q))
        {
            GetInPromotions_TC();
        }
        //MyDebug.Add("index:", GameState.index);
    }


    void OnApplicationPause(bool isPause)
    {
        if (!isPause)
        {
            if (mInpromotions_Picture)
                mInpromotions_Picture.gameObject.SetActive(false);
        }
    }

    public void GetInPromotions_KP()
    {
        blackBack.gameObject.SetActive(true);
        //cancelBtn.gameObject.SetActive(false);
        int kpntj = AppInfo.getSetting("kpntj", 0, 1);

        if (kpntj != 0 && InPromotionList.Count != 0)
        {
            if (kpntj != 3)
            {
                if (kpntj == 1)
                {
                    System.Random random = new System.Random();
                    Picture_Value = random.Next(0, InPromotionList.Count);
                }
                else if (kpntj == 2)
                {
                    Picture_Value = 0;
                }
                mInpromotions_Picture.gameObject.SetActive(true);
                dt = DateTime.Now;
                getImageKP = true;
                StartCoroutine(GetImage(InPromotionList[Picture_Value].Banner, mInpromotions_Picture));
                GameState.isPictureReady = true;
                return;
            }
            else if (kpntj == 3)
            {
                BELog.debug(" Show Google Ads*-*-*-*-*-*-*-*-*-*-*-*-*");
                Ads.showAd(Ads.Type.interstitial);
            }
        }
    }

    private IEnumerator ShowCancel()
    {
        yield return new WaitForSeconds(2f);
        cancelBtn.gameObject.SetActive(true);
    }


    public void GetInPromotions_TC()
    {
        //ZJ_WangSir.CUtils.CloseBannerAd();

        int tcntj = AppInfo.getSetting("tcntj", 0, 1);
        if (tcntj != 0 && InPromotionList.Count != 0)
        {
            GameState.inpType = InpType.TC;
            mInpromotions_TC.SetActive(true);

            if (tcntj == 1)
            {
                System.Random random = new System.Random();
                GameState.index = random.Next(0, InPromotionList.Count);
            }
            else if (tcntj == 2)
            {
                GameState.index = 0;
            }

            StartCoroutine(GetImage(InPromotionList[GameState.index].Icon, mInpromotions_Icon));
            Judge.SetActive(true);
            Cancel_GD.gameObject.SetActive(false);
            GameState.isPictureReady = true;
        }
    }

    public void GetInPromotions_GD()
    {
        int tcntj = AppInfo.getSetting("tcntj", 0, 1);
        if (tcntj != 0 && InPromotionList.Count != 0)
        {
            GameState.isPictureReady = true;
            GameState.inpType = InpType.GD;
            Judge.SetActive(false);
            Cancel_GD.gameObject.SetActive(true);
            if (tcntj == 1)
            {
                System.Random random = new System.Random();
                GameState.index = random.Next(0, InPromotionList.Count);
            }
            else if (tcntj == 2)
            {
                GameState.index = 0;
            }
            StartCoroutine(GetImage(InPromotionList[GameState.index].Icon, mInpromotions_Icon));
        }
    }

    private void CreatPicture()
    {
        for (int i = 0; i < InPromotionList.Count; i++)
        {
            GameObject go = Instantiate(Resources.Load("Picture") as GameObject);
            go.name = "Picture" + i;
            go.transform.parent = MyIdolUIFrameWork.Global.FindChild(mInpromotions_TC.transform, "Content").transform;
            go.transform.localPosition = new Vector2(go.transform.localPosition.x + pictureInterval * i, 0);
            StartCoroutine(GetImage(InPromotionList[i].Banner, go.GetComponent<Image>()));
            content.GetComponent<Drag>().imageList.Add(go.GetComponent<Image>());
            content.GetComponent<Drag>().v3.Add(go.transform.position);
            go.GetComponent<RectTransform>().localScale = new Vector2(pictureRate, pictureRate);

            GameObject unactive = Instantiate(Resources.Load("UnActive") as GameObject);
            unactive.name = "UnActive" + i;
            unactive.transform.parent = MyIdolUIFrameWork.Global.FindChild(transform, "unactive").transform;
            unactive.GetComponent<RectTransform>().localScale = Vector2.one;

            GameObject active = Instantiate(Resources.Load("Active") as GameObject);
            active.name = "Active" + i;
            active.transform.parent = MyIdolUIFrameWork.Global.FindChild(transform, "active").transform;
            active.GetComponent<RectTransform>().localScale = Vector2.one;

            indicators.Add(active);
        }
    }

    public void UpdateIndicators()
    {
        for (int i = 0; i < indicators.Count; i++)
        {
            indicators[i].GetComponent<Image>().enabled = i == GameState.index;
        }
    }

    public void IconToggleEvent()
    {
        //ZJ_WangSir.CUtils.CloseBannerAd();
        content.transform.position = content.transform.GetComponent<Drag>().v3[(InPromotionList.Count - GameState.index) % InPromotionList.Count];
        GameState.inpType = InpType.GD;
        Judge.SetActive(false);
        Cancel_GD.gameObject.SetActive(true);
        StartCoroutine(GetImage(InPromotionList[GameState.index].Banner, mInpromotions_Picture));
        mInpromotions_TC.gameObject.SetActive(true);
    }


    private IEnumerator GetImage(string path, Image picture)
    {
        WWW www = new WWW("file://" + path);
        yield return www;
        if (string.IsNullOrEmpty(www.error))
        {
            Texture2D texture2D = www.texture;
            Sprite sprite = Sprite.Create(texture2D, new Rect(0, 0, texture2D.width, texture2D.height), new Vector2(0, 0));
            picture.sprite = sprite;
        }

        if (blackBack)
            blackBack.gameObject.SetActive(false);

        int tcntj = AppInfo.getSetting("tcntj", 0, 1);
        int kpntj = AppInfo.getSetting("kpntj", 0, 1);
        if (picture = mInpromotions_Icon)
        {
            if (tcntj != 0 && kpntj != 0)
            {
                if (mInpromotions_Icon.GetComponent<Image>().sprite != null)
                    mInpromotions_Icon.gameObject.SetActive(true);
            }
        }

        //if (getImageKP)
        //{
        //    yield return ShowCancel();
        //    getImageKP = false;
        //}
    }

    public void PictureEvent(int index)
    {
        InPromotionList[index].click();
    }

    public void TCPictureEvent()
    {
        InPromotionList[GameState.index].click();
    }

    public void HidePicture()
    {
        mInpromotions_Picture.gameObject.SetActive(false);
        //ZJ_WangSir.CUtils.ShowBannerAd();
    }

    public void CancelGD()
    {
        //ZJ_WangSir.CUtils.ShowBannerAd();
        mInpromotions_TC.gameObject.SetActive(false);
        GameState.index = (GameState.index + 1) % InPromotionList.Count;
        StartCoroutine(GetImage(InPromotionList[GameState.index].Icon, mInpromotions_Icon));
    }
    public void OKEvent()
    {
        Application.Quit();
    }

    public void NOEvent()
    {
        int tcntj = AppInfo.getSetting("tcntj", 0, 1);
        if (tcntj == 1)
        {
            System.Random random = new System.Random();
            GameState.index = random.Next(0, InPromotionList.Count);
        }
        else if (tcntj == 2)
        {
            GameState.index = 0;
        }
        mInpromotions_TC.SetActive(false);
        StartCoroutine(GetImage(InPromotionList[GameState.index].Icon, mInpromotions_Icon));
    }

    public void KPPictureEvent()
    {
        PictureEvent(GameState.index);
    }
}
